package com.offcn.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.offcn.pojo.TCheckitem;
import com.offcn.service.TCheckitemService;
import com.offcn.util.MessageConstant;
import com.offcn.util.PageResult;
import com.offcn.util.QueryPageBean;
import com.offcn.util.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjq
 * @since 2021-08-09
 */
@RestController
@RequestMapping("checkitem")
public class TCheckitemController {

    @Reference
    private TCheckitemService tCheckitemService;

    //列表 -- 分页 -- 条件
    @PostMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean pageBean){
        System.out.println(pageBean);
        try{
            PageResult pageResult = tCheckitemService.findPage(pageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }
    }

    /*@Reference
    private TCheckitemService tCheckitemService;

    //列表 -- 分页 --  条件
    @PostMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean pageBean){
        System.out.println(pageBean);
        try {
            PageResult pageResult = tCheckitemService.findPage(pageBean);
            return  new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }
    }*/

  /*  //添加功能
    @PostMapping("add")
    public Result add(@RequestBody TCheckitem checkitem){
        try{
            tCheckitemService.add(checkitem);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS,null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL,null);
        }
    }*/
    //添加功能
    @PostMapping("add")
    public Result add(@RequestBody TCheckitem checkitem){
        try {
            tCheckitemService.add(checkitem);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS,null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL,null);
        }
    }
    
    //编辑功能之回显
    @GetMapping("findById")
    public Result findById(int id){
        try {
//            TCheckitem checkitem =tCheckitemService.findById(id);
            TCheckitem checkitem = tCheckitemService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }
    }

    //编辑
    @PostMapping("update")
    public Result updata(@RequestBody TCheckitem checkitem){
        try {
            tCheckitemService.update(checkitem);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS,null);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL,null);
        }
    }

    /*//删除
    @GetMapping("delete")
    public Result delete(int id){
        try {
            tCheckitemService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL,null);
        }*/
    //删除功能
    @GetMapping("delete")
    public Result delete(int id){
        try{
            tCheckitemService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL,null);
        }
    }

    //查询所有
    @GetMapping("findAll")
    public Result findAll(){
        try {
            List<TCheckitem> list = tCheckitemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }


    }


}

